<?php
require("menuBarHead.php");
require("db_manage/dbFunctions.php");

$requestor_email = $_POST['select'];
$req_id = getIDByEmail($requestor_email,"Requestors");

if( $req_id == -1 )
	die("Error");
else
{
	$dbh = connectDB();
	$query = "SELECT qr_code, projected_checkin FROM Resource_Designation ".
			"WHERE requestor_id=:req AND has_been_checked_in=0";
	$stmt = $dbh->prepare($query);
	$stmt->bindParam(':req',$req_id);
	$stmt->execute();
	
	if( $stmt->rowCount() == 0 )
		echo "This requestor has no resources currently checked out.";
	else
	{
		$all_qr_codes = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$equipment;
		$rooms;
		foreach( $all_qr_codes as $qr )
		{
			$equip_query = "SELECT * FROM Equipment WHERE qr_code=:qr";
			$rooms_query = "SELECT * FROM Room WHERE qr_code=:qr";

			$stmt = $dbh->prepare($equip_query);
			$stmt->bindParam(':qr',$qr['qr_code']);
			$stmt->execute();
			if( $stmt->rowCount() == 1 )
				$equipment[] = array($stmt->fetch(PDO::FETCH_ASSOC),$qr['projected_checkin']);

			$stmt = $dbh->prepare($rooms_query);
			$stmt->bindParam(':qr',$qr['qr_code']);
			$stmt->execute();
			if( $stmt->rowCount() == 1 )
				$rooms[] = array($stmt->fetch(PDO::FETCH_ASSOC),$qr['projected_checkin']);
		}

$query = "SELECT * FROM Requestors WHERE req_id=:rid";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':rid',$req_id);
$stmt->execute();
$requestor = $stmt->fetch(PDO::FETCH_ASSOC);
$requestor = $requestor['first_name']." ".$requestor['last_name'];

//PRINT EQUIPMENT CHECKED OUT TABLE	
		print("<h2>Equipment Currently Checked Out By $requestor</h2>");
		print("<table border=1><tr><th>QR Code</th><th>Equipment Name</th><th>Serial Number</th><th>Current Condition</th><th>Projected Return Time</th></tr>");
		foreach( $equipment as $e )
		{
			print("<tr>".
					"<td>".$e[0]['qr_code']."</td>".
					"<td>".$e[0]['equipment_name']."</td>".
					"<td>".$e[0]['serial_number']."</td>".
					"<td>".$e[0]['current_condition']."</td>".
					"<td>".$e[1]."</td>".
				  "</tr>\n");
		}
		print("</table>");

//PRINT ROOMS CHECKED OUT TABLE
		print("<h2>Rooms Currently Checked Out By $requestor</h2>");
		print("<table border=1><tr><th>QR Code</th><th>Building</th><th>Floor</th><th>Room Number</th><th>Room Type</th><th>Projected Return Time</th></tr>");
		foreach( $rooms as $r )
		{
			print("<tr>".
					"<td>".$r[0]['qr_code']."</td>".
					"<td>".$r[0]['building']."</td>".
					"<td>".$r[0]['floor']."</td>".
					"<td>".$r[0]['room_number']."</td>".
					"<td>".$r[0]['room_type']."</td>".
					"<td>".$r[1]."</td>".
				  "</tr>\n");
		}
		print("</table>");
	}
															
}

require("menuBarClose.php");
?>
